<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
use Admin\Model\TypeAttribute;
class TypeAttributeController extends CommonController{
	private $model;
	public function __init(){
		//调用父类的构造函数
		parent::__init();
		//实例化类型属性表
		$this->model= new TypeAttribute;
	}
	//首页
	public function index(){
		//获得类型的ID
		$tid = Q('get.tid',0,'intval');
		//获取类型的属性的数量
		$count = $this->model->where("type_tid={$tid}")->count();
		//每10条数据分一页
		$page = Page::row(10)->make($count);
		$data = $this->model->where("type_tid={$tid}")->get();
		View::with('data',$data);
		View::with('page',$page);
		View::make();
	}

	//增加属性
	public function add(){
		//获得类型的ID
		$type_tid = Q('get.type_tid',0,'intval');
		if(IS_AJAX){
			if(!$this->model->store()){
				echo json_encode(array('status'=>0,'message'=>$this->model->getError()));
			}else{
				echo json_encode(array('status'=>1,'message'=>'添加成功'));
			}
			exit;
		}
		
		View::make();
	}
	//删除
	public function del(){
		//获取要删除的ID
		$tid = Q('get.taid',0,'intval');
		$this->model->del($tid);
		go(U('Type/index'));
	}
	//编辑
	public function edit(){	
		if(IS_AJAX){
			if(!$this->model->edit()){
				echo json_encode(array('status'=>0,'message'=>$this->model->getError()));
			}else{
				echo json_encode(array('status'=>1,'message'=>'修改成功'));
			}
			exit;
		}
		//获取旧数据
		$taid = Q('get.taid',0,'intval');
		$oldData = $this->model->where("taid={$taid}")->get();
		View::with('oldData',$oldData[0]);
		View::make();
	}








	
}




?>